/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Service
*        文件名:             OrganizeService.svc.cs
*        创建系统时间:       2012/11/2 19:07:05
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Service
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.ServiceModel;
    using ECP.DataAccess;
    using ECP.DataAccess.Utilities;
    using ECP.Interface;
    using ECP.Model;
    using ECP.Utilities;

    [ServiceBehavior(Namespace = "http://jiangguang.net.cn/")]
    public partial class OrganizeService : MarshalByRefObject, IOrganizeService
    {
        private static OrganizeService instance = null;
        private static object locker = new object();

        public string Add(BaseUserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            string str = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseOrganizeEntity organizeEntity = new BaseOrganizeEntity();
                organizeEntity.GetFrom(dataTable);
                BaseOrganizeDao dao = new BaseOrganizeDao(dbHelper, userInfo);
                str = dao.Add(organizeEntity, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public string AddByDetail(BaseUserInfo userInfo, string parentID, string code, string fullName, string categoryID, string outerPhone, string innerPhone, string fax, bool enabled, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            string str = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseOrganizeDao dao = new BaseOrganizeDao(dbHelper, userInfo);
                str = dao.AddByDetail(parentID, code, fullName, categoryID, outerPhone, innerPhone, fax, enabled, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public int BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseOrganizeDao(dbHelper, userInfo).BatchDelete(ids);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchMoveTo(BaseUserInfo userInfo, string[] organizeIDs, string parentID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseOrganizeDao dao = new BaseOrganizeDao(dbHelper, userInfo);
                for (int i = 0; i < organizeIDs.Length; i++)
                {
                    num += dao.MoveTo(organizeIDs[i], parentID);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchSave(BaseUserInfo userInfo, DataTable dataTable)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseOrganizeDao(dbHelper, userInfo).BatchSave(dataTable);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchSetCode(BaseUserInfo userInfo, string[] ids, string[] codes)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseOrganizeDao(dbHelper, userInfo).BatchSetCode(ids, codes);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchSetSortCode(BaseUserInfo userInfo, string[] ids)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseOrganizeDao(dbHelper, userInfo).BatchSetSortCode(ids);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int Delete(BaseUserInfo userInfo, string id)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseOrganizeDao(dbHelper, userInfo).Delete(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable Get(BaseUserInfo userInfo, string id)
        {
            DataTable table = new DataTable(BaseOrganizeTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseOrganizeDao(dbHelper, userInfo).Get(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataTable GetChildrenStaffs(BaseUserInfo userInfo, string organizeID)
        {
            DataTable childrenStaffs = new DataTable(BaseOrganizeTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                childrenStaffs = new BaseOrganizeDao(dbHelper, userInfo).GetChildrenStaffs(organizeID);
                childrenStaffs.DefaultView.Sort = BaseStaffTable.FieldSortCode;
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return childrenStaffs;
        }

        public DataTable GetCompanyList(BaseUserInfo userInfo, string organizeID)
        {
            DataTable companyList = new DataTable(BaseOrganizeTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                companyList = new BaseOrganizeDao(dbHelper, userInfo).GetCompanyList(organizeID);
                companyList.DefaultView.Sort = BaseOrganizeTable.FieldSortCode;
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return companyList;
        }

        public DataTable GetDepartmentList(BaseUserInfo userInfo, string organizeID)
        {
            DataTable departmentList = new DataTable(BaseOrganizeTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                departmentList = new BaseOrganizeDao(dbHelper, userInfo).GetDepartmentList(organizeID);
                departmentList.DefaultView.Sort = BaseOrganizeTable.FieldSortCode;
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return departmentList;
        }

        public DataTable GetInnerOrganize(BaseUserInfo userInfo, string organizeID)
        {
            DataTable innerOrganize = new DataTable(BaseOrganizeTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                innerOrganize = new BaseOrganizeDao(dbHelper, userInfo).GetInnerOrganize(organizeID);
                innerOrganize.DefaultView.Sort = BaseOrganizeTable.FieldSortCode;
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return innerOrganize;
        }

        public DataTable GetList(BaseUserInfo userInfo)
        {
            DataTable dT = new DataTable(BaseOrganizeTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dT = new BaseOrganizeDao(dbHelper, userInfo).GetDT();
                dT.DefaultView.Sort = BaseOrganizeTable.FieldSortCode;
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public DataTable GetListByParent(BaseUserInfo userInfo, string parentID)
        {
            DataTable dTByParent = new DataTable(BaseOrganizeTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dTByParent = new BaseOrganizeDao(dbHelper, userInfo).GetDTByParent(parentID);
                dTByParent.DefaultView.Sort = BaseOrganizeTable.FieldSortCode;
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dTByParent;
        }

        public DataTable GetParentChildrenStaffs(BaseUserInfo userInfo, string organizeID)
        {
            DataTable parentChildrenStaffs = new DataTable(BaseOrganizeTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                parentChildrenStaffs = new BaseOrganizeDao(dbHelper, userInfo).GetParentChildrenStaffs(organizeID);
                parentChildrenStaffs.DefaultView.Sort = BaseStaffTable.FieldSortCode;
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return parentChildrenStaffs;
        }

        public void Load()
        {
        }

        public int MoveTo(BaseUserInfo userInfo, string organizeID, string parentID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseOrganizeDao(dbHelper, userInfo).MoveTo(organizeID, parentID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable Search(BaseUserInfo userInfo, string organizeID, string search)
        {
            DataTable table = new DataTable(BaseOrganizeTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseOrganizeDao(dbHelper, userInfo).Search(string.Empty, search);
                table.DefaultView.Sort = BaseOrganizeTable.FieldSortCode;
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public int Update(BaseUserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseOrganizeEntity organizeEntity = new BaseOrganizeEntity();
                organizeEntity.GetFrom(dataTable);
                BaseOrganizeDao dao = new BaseOrganizeDao(dbHelper, userInfo);
                num = dao.Update(organizeEntity, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public static OrganizeService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new OrganizeService();
                        }
                    }
                }
                return instance;
            }
        }
    }
}